import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-01
 * Time: 0:58
 */
public class Permute {
    static List<List<Integer>> ret;
    static List<Integer> path;
    static int[] check;
    static int flag;

    public static List<List<Integer>> permute(int[] nums) {
        ret=new ArrayList<>();
        path=new ArrayList<>();
        check = new int[nums.length];
        dfs(nums);
        return ret;
    }

    private static void dfs(int[] nums) {
        if (path.size() == check.length) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (check[i] == 0) {
                path.add(nums[i]);
                check[i]=1;
                dfs(nums);
                //回溯
                check[i]=0;
                path.remove(path.size()-1);
            }
        }
        return;
    }
}
